package cj.web.kly.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.kly.domain.SourceInfor;
import cj.web.kly.service.SourceInforService;
import cj.web.login.controllers.BaseController;

@Path("/sourceInfor")
public class SourceInforController extends BaseController{

	private static Logger logger = Logger.getLogger(SourceInforController.class.getName());
	
	@Autowired
	private SourceInforService sourceInforService;
	
	@Get("show")
	public String show(Model model,SourceInfor subject) {
		return "/views/kly/sourceInforList.vm";
	}

	@Post("getList")
	public String queryList(SourceInfor subject) {
	
		//当前页  
		subject.setStart((subject.getPage()-1)*subject.getRows());
		subject.setLength(subject.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = sourceInforService.query(subject);
		List<SourceInfor> objs = (List<SourceInfor>) rep.get("sourceInforlis");
		
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model){
		return "/views/kly/sourceInforDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,SourceInfor subject,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		subject.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		subject.setCreateUser(returnSessionObj(request).getAccount());
		subject.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			sourceInforService.create(subject);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		SourceInfor subject = sourceInforService.queryById(id);

		model.add("subject", subject);
		return "/views/kly/sourceInforEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SourceInfor subject,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		subject.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			sourceInforService.update(subject);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			sourceInforService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}